load OffsetTestData.mat

mdlTemp = fitlm(OffsetVsTempData(:,1),OffsetVsTempData(:,2));% Apply linear regression fit to Offset Vs Temperature data
mdlGrad = fitlm(OffsetVsGradData(:,1),OffsetVsGradData(:,2));% Apply linear regression fit to Native Offset Vs Gradient data
mdlGradCorrect = fitlm(OffsetVsGradDataCorrected(:,1),OffsetVsGradDataCorrected(:,2));% Apply linear regression fit to Corrected Offset Vs Temperature data
disp('Fits to Temperature, Raw Thermal Gradient and Corrected Thermal Gradient Data:')
tiledlayout(3,1)
nexttile % Plot Offset Vs Temperature data
errorbar(OffsetVsTempData(:,1),OffsetVsTempData(:,2),OffsetVsTempError,OffsetVsTempError,TempError(1:6),TempError(1:6),'o')
grid on 
hold on
plot(OffsetVsTempData(:,1),(OffsetVsTempData(:,1)*mdlTemp.Coefficients.Estimate(2))+mdlTemp.Coefficients.Estimate(1),'--b');
label = join([num2str(mdlTemp.Coefficients.Estimate(2)),'+/-', num2str(mdlTemp.Coefficients.SE(2)*2),' nT/C']); % label for legend including error of fit
xlabel('Temperature in C')
legend('Offset', label)
title('Offset VS Temperature')
disp(label)
hold off

nexttile % Plot Raw Offset Vs Thermal Gradient data
errorbar(OffsetVsGradData(:,1),OffsetVsGradData(:,2),OffsetVsGradError,OffsetVsGradError,TempError,TempError,'o')
hold on
grid on
plot(OffsetVsGradData(:,1),(OffsetVsGradData(:,1)*mdlGrad.Coefficients.Estimate(2))+mdlGrad.Coefficients.Estimate(1),'--b');
label  = join([num2str(mdlGrad.Coefficients.Estimate(2)),'+/-', num2str(mdlGrad.Coefficients.SE(2)*2),' nT/C']);% label for legend including error of fit
xlabel('Thermal Gradient in C')
legend('Offset', label )
title('Offset VS Gradient (Raw)')
disp(label)
hold off

nexttile % Plot Corrected Offset Vs Thermal Gradient data
errorbar(OffsetVsGradDataCorrected(:,1),OffsetVsGradDataCorrected(:,2),OffsetVsGradError,OffsetVsGradError,TempError,TempError,'o') 
hold on
plot(OffsetVsGradDataCorrected(:,1),(OffsetVsGradDataCorrected(:,1)*mdlGradCorrect.Coefficients.Estimate(2))+mdlGradCorrect.Coefficients.Estimate(1)+0.04,'--b');
label = join([num2str(mdlGradCorrect.Coefficients.Estimate(2)),'+/-', num2str(mdlGradCorrect.Coefficients.SE(2)*2),' nT/C']); % label for legend including error of fit
xlabel('Thermal Gradient in C')
legend('Offset', label)
title('Offset VS Gradient (Corrected)')
ylabel('Thermal Gradient in C')
disp(label)
grid on;